package com.lx.wx.controller;

import com.lx.bean.Message;
import com.lx.bean.User;
import com.lx.mvc.ResponseBody;
import com.lx.mvc.ResponseView;
import com.lx.util.JsonUtil;
import com.lx.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: QRCodeController
 * @Description:
 * @Date Created in  2021-07-19 9:09
 * @Modified By:
 * @Author: 零巽
 */
public class QRCodeController {
    @ResponseView("/wx/createQRCode.do")
    public String CreateQRCode(HttpServletRequest req, HttpServletResponse resp) {
        String code = req.getParameter("code");
        String type = req.getParameter("type");
        String qRCodeContent = null;
        if ("user".equals(type)) {
            User user = UserUtil.getUser(req.getSession());
            String phone = user.getPhone();
            qRCodeContent = type + phone;
        } else if ("express".equals(type)) {
            qRCodeContent = type+code;
        }
        HttpSession session = req.getSession();
        session.setAttribute("qrcode",qRCodeContent);

        return "/personQRcode.html";
    }

    @ResponseBody("/wx/getQRCode.do")
    public static String getQRCode(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message message = new Message();
        if (qrcode == null) {
            message.setStatus(-1);
            message.setResult("取件码获取错误，请尝试重新操作！");
        } else {
            message.setStatus(0);
            message.setResult(qrcode);
        }
        return JsonUtil.toJSON(message);
    }
}
